/*

Copyright (C) 2010  Christoph Schnack 

    This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/

package VocabularyManagement;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;
import GuiTools.*;
import Mainframe.Mainframe;
import Settings.Language;
import Settings.Settings;
import Statistics.Statistics.WordStatistic;
import Storage.*;

/**
 * 
 * Dialog asking for all possible Word properties. Is runnable in editing or creating mode. 
 * 
 * @author Christoph Schnack
 *
 */

public class EditWordDialog{
	boolean edit, RETURNTOPREVIOUSDIALOG=true;
	TribuitFrame ewdFrame;
	String vocToEdit,p1,p2;
	TribuitTextField stammATF,stammBTF,ewdLectionTF;
	JComboBox tcACB,tcBCB,ewdLectionCB;
	JRadioButton toLectRB,newLectRB;
	GridBagConstraints ewdGBC;
	LabelPanel tcBLP, tcALP;
	Word old;
	Settings SET;
	Language LANG;
	Storage MAINVOC;
    Mainframe MAINFRAME;
    MDManager VMDM;
	Vocabulary vte;
	JPanel ewdFormularPanel;
	MiniButton addMeaningABtn,addMeaningBBtn;
	Vector<EWDFormularPanel> formularPanelsA = new Vector<EWDFormularPanel>(),formularPanelsB=new Vector<EWDFormularPanel>();
	Vector<Meaning> meaningsA = new Vector<Meaning>(),meaningsB = new Vector<Meaning>();
	JTabbedPane jtbA,jtbB;
	JButton ewdCancel,ewdOk;
	
	/**
	 * 
	 * @param v vocabulary to edit
	 */
	public EditWordDialog(String v,Storage mainvoc,Settings set,Mainframe mf,MDManager vmdm){
		MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
    	VMDM=vmdm;
		edit = false;
		vte = MAINVOC.getVocabulary(v);
		vocToEdit = v;
		meaningsA.add(new Meaning());
		meaningsB.add(new Meaning());
		buildEditWordDialog(v);
		if(!vte.hasWords()) ewdLectionCB.addItem(LANG.getUnknown());
		ewdLectionCB.setSelectedIndex(ewdLectionCB.getItemCount()-1);
	}
	
	public EditWordDialog(String v,Storage mainvoc,Settings set,Mainframe mf){
		MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME = mf;
    	RETURNTOPREVIOUSDIALOG=false;
		edit = false;
		vte = MAINVOC.getVocabulary(v);
		vocToEdit = v;
		meaningsA.add(new Meaning());
		meaningsB.add(new Meaning());
		buildEditWordDialog(v);
		if(!vte.hasWords()) ewdLectionCB.addItem(LANG.getUnknown());
		ewdLectionCB.setSelectedIndex(ewdLectionCB.getItemCount()-1);
	}
	
	/**
	 * 
	 * @param v vocabulary to edit
	 * @param wd word to edit
	 */
	public EditWordDialog(String v,Word wd,Storage mainvoc,Settings set,Mainframe mf,MDManager vmdm){
		MAINVOC=mainvoc;
    	SET=set;
    	LANG=SET.getLanguage();
    	MAINFRAME=mf;
    	VMDM=vmdm;
		edit=true;
		vte = MAINVOC.getVocabulary(v);
		vocToEdit = v;
		old = wd;
		meaningsA = (Vector<Meaning>)old.getMeaningVectorA().clone();
		meaningsB = (Vector<Meaning>)old.getMeaningVectorB().clone();
		buildEditWordDialog(v);
		ewdLectionCB.setSelectedItem(wd.getLection());
	}
	public void buildEditWordDialog(String v){
		String title = LANG.getAddWord();
		if(edit) title = LANG.getEditWord();
		if(RETURNTOPREVIOUSDIALOG)ewdFrame = new MDTribuitFrame(title,VMDM);
		else ewdFrame = new TribuitFrame(title);
		
	//section panel
		ewdLectionTF = new TribuitTextField(SET,3);
		String[] lectContent = vte.getLections();
		ewdLectionCB = new JComboBox(lectContent!=null?lectContent:new String[0]);
		ewdLectionCB.setPreferredSize(new Dimension(100,26));
		toLectRB = new JRadioButton(LANG.getSelect());
		toLectRB.setOpaque(false);
		toLectRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ewdLectionTF.setEnabled(false);
				ewdLectionCB.setEnabled(true);
			}
		});
		newLectRB = new JRadioButton(LANG.getNew());
		newLectRB.setOpaque(false);
		newLectRB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ewdLectionTF.setEnabled(true);
				ewdLectionTF.grabFocus();
				ewdLectionCB.setEnabled(false);
			}
		});
		ButtonGroup lBG = new ButtonGroup();
		lBG.add(toLectRB);
		lBG.add(newLectRB);
		if(lectContent==null){
			toLectRB.setEnabled(false);
			newLectRB.doClick();
		}
		else{
			for(int i=0;i<lectContent.length;i++){
				if(lectContent[i].equals("~")){
					lectContent[i]=LANG.getUnknown();
				}
			}
			ewdLectionCB = new JComboBox(lectContent);
			ewdLectionCB.setPreferredSize(new Dimension(100,26));
			toLectRB.doClick();
		}
		JPanel lectSelectPanel = new JPanel();
		lectSelectPanel.setLayout(new GridLayout(2,2));
		lectSelectPanel.add(toLectRB);
		lectSelectPanel.add(newLectRB);
		lectSelectPanel.add(ewdLectionCB);
		lectSelectPanel.add(ewdLectionTF);
		lectSelectPanel.setOpaque(false);
		lectSelectPanel.setBorder(BorderFactory.createTitledBorder(null,LANG.getLection()+":"));
		
		ewdOk = new JButton(LANG.getAccept());
		ewdOk.addActionListener(new ewdOkListener());
		ewdCancel = new JButton(LANG.getCancel());
		ewdCancel.addActionListener(new ewdCancelListener());
		JPanel ewdBtnPanel = new JPanel();
		ewdBtnPanel.add(ewdOk);
		ewdBtnPanel.add(ewdCancel);
		ewdBtnPanel.setBackground(GuiTools.BGCOLOR);
		
	//term class panel
		tcACB = new JComboBox(vte.getSortedTermClassKeysA());
		tcACB.setPreferredSize(new Dimension(300,26));
		tcACB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String selection = (String)tcACB.getSelectedItem();
				for(EWDFormularPanel formularPanel : formularPanelsA){
					formularPanel.setTermClass(vte.getTermclassA(selection),selection);
				}
				if(ewdFrame.isVisible()) ewdFrame.packAndKeepBounds();
				else ewdFrame.pack();
			}
		});
		tcBCB = new JComboBox(vte.getSortedTermClassKeysB());
		tcBCB.setPreferredSize(new Dimension(300,26));
		tcBCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String selection = (String)tcBCB.getSelectedItem();
				for(EWDFormularPanel formularPanel : formularPanelsB){
					formularPanel.setTermClass(vte.getTermclassB(selection),selection);
				}
				if(ewdFrame.isVisible()) ewdFrame.packAndKeepBounds();
				else ewdFrame.pack();
			}
		});
		tcALP = new LabelPanel(LANG.getTermClass()+":", tcACB);
		tcALP.setOpaque(false);
		tcBLP = new LabelPanel(LANG.getTermClass()+":", tcBCB);
		tcBLP.setOpaque(false);
		JLabel ALabel = new JLabel(vte.getNameA());
		ALabel.setPreferredSize(new Dimension(300,ALabel.getPreferredSize().height));
		ALabel.setHorizontalAlignment(JLabel.CENTER);
		ALabel.setFont(new Font("Serif", Font.BOLD, 20));
		JLabel BLabel = new JLabel(vte.getNameB());
		BLabel.setFont(new Font("Serif", Font.BOLD, 20));
		BLabel.setPreferredSize(new Dimension(300,BLabel.getPreferredSize().height));
		BLabel.setHorizontalAlignment(JLabel.CENTER);
		
	//stat-info
		JLabel correctsLabel = new JLabel(LANG.getCorrect()+": "),wrongsLabel = new JLabel(LANG.getWrong()+": "),lastCheckedLabel = new JLabel(LANG.getChecked()+": "),corrects=new JLabel("n.a."),wrongs=new JLabel("n.a."),lastChecked = new JLabel("n.a.");
		Font font = new Font(corrects.getFont().getFontName(),Font.PLAIN,corrects.getFont().getSize());
		corrects.setFont(font);
		wrongs.setFont(font);
		lastChecked.setFont(font);
		JPanel statPanel = new JPanel();
		statPanel.setLayout(new GridLayout(3,3));
		statPanel.add(correctsLabel);
		statPanel.add(corrects);
		statPanel.add(wrongsLabel);
		statPanel.add(wrongs);
		statPanel.add(lastCheckedLabel);
		statPanel.add(lastChecked);
		statPanel.setOpaque(false);
		if(edit && new File(GuiTools.STATISTICFILEPATH).exists()){
			HashMap<String,WordStatistic> vocStats = MAINVOC.getStatistics().getVocabularyStatistics(vocToEdit,true);
			if(!vocStats.isEmpty()){
				corrects.setText(vocStats.get(old.getName()).getCorrect()+"");
				wrongs.setText(vocStats.get(old.getName()).getWrong()+"");
				long date = vocStats.get(old.getName()).getLastCheckedDate();
				if(date!=0){
					Calendar c = Calendar.getInstance();
					c.setTime(new Date(date));
					lastChecked.setText(c.get(Calendar.DAY_OF_MONTH)+"."+c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())+"."+c.get(Calendar.YEAR) );
				}
			}
		}
		JPanel statLP = new JPanel();
		statLP.add(statPanel);
		statLP.setOpaque(false);
		statLP.setBorder(BorderFactory.createTitledBorder(null,LANG.getStatistics()+":"));
		
	//tabbed pane
		jtbA = new JTabbedPane();
		jtbB = new JTabbedPane();
		
	//meaning buttons
		addMeaningABtn = new MiniButton(GuiTools.MINI_PLUS_ICON,GuiTools.MINI_PLUS_HOVERED_ICON,GuiTools.MINI_PLUS_DISABLED_ICON,true,new AddMeaningABtnListener());
		addMeaningABtn.setToolTipText(LANG.getToolTipTextAddMeaning()+" (STRG + N)");
		MiniButton delMeaningABtn = new MiniButton(GuiTools.MINI_MINUS_ICON,GuiTools.MINI_MINUS_HOVERED_ICON,GuiTools.MINI_MINUS_DISABLED_ICON,true,new DeleteMeaningABtnListener());
		delMeaningABtn.setToolTipText(LANG.getToolTipTextDeleteMeaning());
		
		addMeaningBBtn = new MiniButton(GuiTools.MINI_PLUS_ICON,GuiTools.MINI_PLUS_HOVERED_ICON,GuiTools.MINI_PLUS_DISABLED_ICON,true,new AddMeaningBBtnListener());
		addMeaningBBtn.setToolTipText(LANG.getToolTipTextAddMeaning()+" (STRG + SHIFT +  N)");
		MiniButton delMeaningBBtn = new MiniButton(GuiTools.MINI_MINUS_ICON,GuiTools.MINI_MINUS_HOVERED_ICON,GuiTools.MINI_MINUS_DISABLED_ICON,true,new DeleteMeaningBBtnListener());
		delMeaningBBtn.setToolTipText(LANG.getToolTipTextDeleteMeaning());
		
		JPanel btnPanelA = new JPanel(new FlowLayout(FlowLayout.LEFT)),btnPanelB = new JPanel(new FlowLayout(FlowLayout.LEFT));
		btnPanelA.add(addMeaningABtn);
		btnPanelA.add(delMeaningABtn);
		btnPanelA.setOpaque(false);
		btnPanelB.add(addMeaningBBtn);
		btnPanelB.add(delMeaningBBtn);
		btnPanelB.setOpaque(false);
		
		ewdFormularPanel = new JPanel();
		ewdFormularPanel.setLayout(new GridBagLayout());
		ewdFormularPanel.setBackground(Color.LIGHT_GRAY);
		ewdGBC = new GridBagConstraints();
		ewdGBC.insets = new Insets(0,10,0,10);
		ewdGBC.anchor = GridBagConstraints.CENTER;
		ewdGBC.fill=GridBagConstraints.NONE;
		ewdGBC.weighty = 1;
		ewdGBC.weightx = 1;
		ewdGBC.gridy = 0;
		ewdFormularPanel.add(ALabel,ewdGBC);
		ewdGBC.fill=GridBagConstraints.BOTH;
		ewdGBC.anchor = GridBagConstraints.NORTHWEST;
		ewdGBC.gridy = 1;
		ewdFormularPanel.add(tcALP,ewdGBC);
		ewdGBC.gridy = 2;
		ewdGBC.insets = new Insets(10,10,10,10);
		ewdFormularPanel.add(jtbA,ewdGBC);
		ewdGBC.gridy = 3;
		ewdGBC.insets = new Insets(0,10,0,10);
		ewdFormularPanel.add(btnPanelA,ewdGBC);
		ewdGBC.gridy = 4;
		ewdGBC.fill=GridBagConstraints.VERTICAL;
		ewdFormularPanel.add(lectSelectPanel,ewdGBC);
		ewdGBC.anchor = GridBagConstraints.CENTER;
		ewdGBC.fill=GridBagConstraints.NONE;
		ewdGBC.gridwidth=1;
		ewdGBC.gridy = 0;
		ewdGBC.gridx = 2;
		ewdFormularPanel.add(BLabel,ewdGBC);
		ewdGBC.fill=GridBagConstraints.BOTH;
		ewdGBC.anchor = GridBagConstraints.NORTHWEST;
		ewdGBC.gridy = 1;
		ewdFormularPanel.add(tcBLP,ewdGBC);
		ewdGBC.gridy = 2;
		ewdGBC.insets = new Insets(10,10,10,10);
		ewdFormularPanel.add(jtbB,ewdGBC);
		ewdGBC.insets = new Insets(0,10,0,0);
		ewdGBC.gridy = 3;
		ewdFormularPanel.add(btnPanelB,ewdGBC);
		ewdGBC.gridy = 4;
		ewdGBC.fill=GridBagConstraints.VERTICAL;
		ewdGBC.anchor = GridBagConstraints.NORTHWEST;
		if(edit)ewdFormularPanel.add(statLP,ewdGBC);
		
		
		
		ewdFrame.getContentPane().add(BorderLayout.NORTH,new Header(title));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		if(screenSize.getHeight()<768){
			JScrollPane jsp = new JScrollPane(ewdFormularPanel);
			ewdFrame.getContentPane().add(BorderLayout.CENTER,jsp);
		}
		else ewdFrame.getContentPane().add(BorderLayout.CENTER,ewdFormularPanel);
		ewdFrame.getContentPane().add(BorderLayout.SOUTH,ewdBtnPanel);
		ewdFrame.setBackground(GuiTools.BGCOLOR);
		if(!edit){
			tcACB.setSelectedIndex(0);
			tcBCB.setSelectedIndex(0);
		}
		else{
			title = LANG.getEditWord();
			ewdCancel.setText(LANG.getDiscardAndCancel());
			tcACB.setSelectedItem(old.getTermclassA());
			tcBCB.setSelectedItem(old.getTermclassB());
		}
		ewdFrame.setTitle(title);
		ewdFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				if(RETURNTOPREVIOUSDIALOG)new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
			}
		});
		
		ewdFrame.setFocusable(true);
		ewdFrame.setResizable(true);
		setTabbedPanes();
		setShortCuts();
		formularPanelsA.firstElement().grabFocusFirstComponent();
		ewdFrame.showUpAt(MAINFRAME);
	}

	public void setTabbedPanes(){
		formularPanelsA.removeAllElements();
		jtbA.removeAll();
		EWDFormularPanel fp;
		int i = 1;
		for(Meaning m : meaningsA){
			fp = new EWDFormularPanel(ewdFrame, SET, m, vte.getTermclassA((String)tcACB.getSelectedItem()), vte.getCategoriesA(), i);
			formularPanelsA.add(fp);
			jtbA.add((i++)+"",fp);
		}
		formularPanelsB.removeAllElements();
		jtbB.removeAll();
		i = 1;
		for(Meaning m : meaningsB){
			fp = new EWDFormularPanel(ewdFrame, SET, m, vte.getTermclassB((String)tcBCB.getSelectedItem()), vte.getCategoriesB(), i);
			formularPanelsB.add(fp);
			jtbB.add((i++)+"",fp);
		}
		ewdFrame.pack();
		ewdFrame.repaint();
	}
	
	class ewdOkListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			for(EWDFormularPanel fp : formularPanelsA){
				if(!fp.checkInputs()){
					JOptionPane.showMessageDialog(ewdFrame,vte.getNameA()+", "+ fp.getErrorMessage(), LANG.getError(), JOptionPane.WARNING_MESSAGE);
					return;
				}
			}
			for(EWDFormularPanel fp : formularPanelsB){
				if(!fp.checkInputs()){
					JOptionPane.showMessageDialog(ewdFrame,vte.getNameB()+", "+ fp.getErrorMessage(), LANG.getError(), JOptionPane.WARNING_MESSAGE);
					return;
				}
			}
			String lection = "";
			if(toLectRB.isSelected()){
				lection = (String) ewdLectionCB.getSelectedItem();
				if(lection==null) lection=LANG.getUnknown();
			}
			else{
				lection = ewdLectionTF.getText();
				if(!GuiTools.checkInput(lection)){
					JOptionPane.showMessageDialog(ewdFrame, LANG.getNotThoseSigns()+ " :  ~  ;  %  =  :  >", LANG.getError(), JOptionPane.WARNING_MESSAGE);
					ewdLectionTF.requestFocus();
					ewdLectionTF.selectAll();
					return;
				}
				if(lection.equals("")){
					int ans = JOptionPane.showOptionDialog(ewdFrame, LANG.getWithoutLection(), LANG.getQuestion(), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,null, null,null);
					if(ans==JOptionPane.YES_OPTION) lection = LANG.getUnknown();
					else return;
				}
			}
			Vector<Meaning> mngsA = new Vector<Meaning>();
			for(EWDFormularPanel fp : formularPanelsA){
				mngsA.add(fp.getMeaning());
			}
			Vector<Meaning> mngsB = new Vector<Meaning>();
			for(EWDFormularPanel fp : formularPanelsB){
				mngsB.add(fp.getMeaning());
			}
			Word word = MAINVOC.makeWord(lection,(String)tcACB.getSelectedItem(),(String)tcBCB.getSelectedItem(),mngsA,mngsB);//m1,p1,tcA,m2,p2,tcB,n1,n2,metaA,metaB);
			if(edit){
				if(MAINFRAME.getMenu().getWordcheckRunning()){
					JOptionPane.showMessageDialog(ewdFrame, LANG.getQuitWordcheckBeforeAlteringWordData(),LANG.getAttention(),JOptionPane.WARNING_MESSAGE);
					return;
				}
				else{
					JPanel panel = new JPanel();
					panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
					JLabel question = new JLabel( LANG.getOverwrite() +" " +  old.getName()+"  ?");
					JCheckBox keepStatsCB = new JCheckBox(LANG.getKeepStatistics(),true);
					panel.add(question);
					panel.add(keepStatsCB);
					int answer = JOptionPane.showOptionDialog(ewdFrame,panel, LANG.getOverwrite()+"?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,null, null,null);
					if(answer==JOptionPane.YES_OPTION){
						if((!vte.hasWord(word.getTermA(),word.getTermB()))) MAINVOC.editWord(vocToEdit,old,word,keepStatsCB.isSelected());
						else{
							String newName = word.getName(); 
							if(!newName.equals(old.getName())) MAINVOC.deleteWord(vocToEdit,newName);
							MAINVOC.editWord(vocToEdit,old,word,keepStatsCB.isSelected());
						}
					}
					else return;
				}
			}
			else{
				if((!vte.hasWord(word.getTermA(),word.getTermB()))){
					MAINVOC.addWordToVoc(vocToEdit,word);
					MAINFRAME.updateMainframePane(MAINVOC,SET);
				}
				else{
					JOptionPane.showMessageDialog(ewdFrame, LANG.getWordAlreadyExists());
					return;
				}
			}
			ewdFrame.dispose();
			if(RETURNTOPREVIOUSDIALOG)new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
		}
	}
	
	class ewdCancelListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			ewdFrame.dispose();
			if(RETURNTOPREVIOUSDIALOG)new EditVocDialog(MAINVOC,SET,MAINFRAME,VMDM);
		}
	}
	
	class AddMeaningABtnListener implements TAction{
		public void perform(){
			meaningsA.add(new Meaning());
			formularPanelsA.add(new EWDFormularPanel(ewdFrame, SET, meaningsA.lastElement(), vte.getTermclassA((String)tcACB.getSelectedItem()), vte.getCategoriesA(), meaningsA.size()));
			jtbA.addTab(meaningsA.size()+"", formularPanelsA.lastElement());
			jtbA.setSelectedIndex(jtbA.getTabCount()-1);
			((EWDFormularPanel)jtbA.getComponentAt(jtbA.getSelectedIndex())).grabFocusFirstComponent();
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					((EWDFormularPanel)jtbA.getComponentAt(jtbA.getSelectedIndex())).grabFocusFirstComponent();
				}
			});
		}
	}
	class AddMeaningBBtnListener implements TAction{
		public void perform(){
			meaningsB.add(new Meaning());
			formularPanelsB.add(new EWDFormularPanel(ewdFrame, SET, meaningsB.lastElement(), vte.getTermclassB((String)tcBCB.getSelectedItem()), vte.getCategoriesB(), meaningsB.size()));
			jtbB.addTab(meaningsB.size()+"", formularPanelsB.lastElement());
			jtbB.setSelectedIndex(jtbB.getTabCount()-1);
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					((EWDFormularPanel)jtbB.getComponentAt(jtbB.getSelectedIndex())).grabFocusFirstComponent();
				}
			});
		}
	}
	
	class DeleteMeaningABtnListener implements TAction{
		public void perform(){
			if(meaningsA.size()>1){
				meaningsA.removeElementAt(jtbA.getSelectedIndex());
				formularPanelsA.removeElementAt(jtbA.getSelectedIndex());
				jtbA.removeTabAt(jtbA.getSelectedIndex());
				for(int i=0;i<jtbA.getTabCount();i++){
					jtbA.setTitleAt(i, (i+1)+"");
					formularPanelsA.elementAt(i).setMeaningTFTitle((i+1)+":");
				}
				((EWDFormularPanel)jtbA.getComponentAt(jtbA.getSelectedIndex())).grabFocusFirstComponent();
			}
		}
	}
	class DeleteMeaningBBtnListener implements TAction{
		public void perform(){
			if(meaningsB.size()>1){
				meaningsB.removeElementAt(jtbB.getSelectedIndex());
				formularPanelsB.removeElementAt(jtbB.getSelectedIndex());
				jtbB.removeTabAt(jtbB.getSelectedIndex());
				for(int i=0;i<jtbB.getTabCount();i++){
					jtbB.setTitleAt(i, (i+1)+"");
					formularPanelsB.elementAt(i).setMeaningTFTitle((i+1)+":");
				}
				((EWDFormularPanel)jtbB.getComponentAt(jtbB.getSelectedIndex())).grabFocusFirstComponent();
			}
		}
	}
	
	public void setShortCuts(){
		InputMap inputMap = ewdFormularPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap actionMap = ewdFormularPanel.getActionMap(); 
		ewdFormularPanel.setFocusable(true);
		inputMap.put(KeyStroke.getKeyStroke("ctrl S"), "openSynonymsA");
		actionMap.put("openSynonymsA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				formularPanelsA.get(jtbA.getSelectedIndex()).openSynonymsDialog();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("ctrl shift S"), "openSynonymsB");
		actionMap.put("openSynonymsB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				formularPanelsB.get(jtbB.getSelectedIndex()).openSynonymsDialog();
			}
		});
		
		inputMap.put(KeyStroke.getKeyStroke("ctrl N"), "newMeaningA");
		actionMap.put("newMeaningA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				addMeaningABtn.doClick();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("ctrl shift N"), "newMeaningB");
		actionMap.put("newMeaningB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				addMeaningBBtn.doClick();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("ctrl D"), "cancel");
		actionMap.put("cancel", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				ewdCancel.doClick();
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("ctrl ENTER"), "accept");
		actionMap.put("accept", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				ewdOk.doClick();
			}
		});
	//switchMeanings
		inputMap.put(KeyStroke.getKeyStroke("alt LEFT"), "switchTabLeftA");
		actionMap.put("switchTabLeftA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(jtbA.getSelectedIndex()>0){
					jtbA.setSelectedIndex(jtbA.getSelectedIndex()-1);
					((EWDFormularPanel)jtbA.getComponentAt(jtbA.getSelectedIndex())).grabFocusFirstComponent();
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("alt RIGHT"), "switchTabRightA");
		actionMap.put("switchTabRightA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(jtbA.getSelectedIndex()<jtbA.getTabCount()-1){
					jtbA.setSelectedIndex(jtbA.getSelectedIndex()+1);
					((EWDFormularPanel)jtbA.getComponentAt(jtbA.getSelectedIndex())).grabFocusFirstComponent();
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("alt shift LEFT"), "switchTabLeftB");
		actionMap.put("switchTabLeftB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(jtbB.getSelectedIndex()>0){
					jtbB.setSelectedIndex(jtbB.getSelectedIndex()-1);
					((EWDFormularPanel)jtbB.getComponentAt(jtbB.getSelectedIndex())).grabFocusFirstComponent();
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("alt shift RIGHT"), "switchTabRightB");
		actionMap.put("switchTabRightB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(jtbB.getSelectedIndex()<jtbB.getTabCount()-1){
					jtbB.setSelectedIndex(jtbB.getSelectedIndex()+1);
					((EWDFormularPanel)jtbB.getComponentAt(jtbB.getSelectedIndex())).grabFocusFirstComponent();
				}
			}
		});
	//TermClass
		//A
		inputMap.put(KeyStroke.getKeyStroke("alt UP"), "tcUpA");
		actionMap.put("tcUpA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(tcACB.getSelectedIndex()>0){
					tcACB.setSelectedIndex(tcACB.getSelectedIndex()-1);
					formularPanelsA.get(jtbA.getSelectedIndex()).grabFocusFirstComponent();
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("alt DOWN"), "tcDownA");
		actionMap.put("tcDownA", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(tcACB.getSelectedIndex()<tcACB.getItemCount()-1){
					tcACB.setSelectedIndex(tcACB.getSelectedIndex()+1);
					formularPanelsA.get(jtbA.getSelectedIndex()).grabFocusFirstComponent();
				}
			}
		});
		//B
		inputMap.put(KeyStroke.getKeyStroke("alt shift UP"), "tcUpB");
		actionMap.put("tcUpB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(tcBCB.getSelectedIndex()>0){
					tcBCB.setSelectedIndex(tcBCB.getSelectedIndex()-1);
					formularPanelsB.get(jtbB.getSelectedIndex()).grabFocusFirstComponent();
				}
			}
		});
		inputMap.put(KeyStroke.getKeyStroke("alt shift DOWN"), "tcDownB");
		actionMap.put("tcDownB", new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				if(tcBCB.getSelectedIndex()<tcBCB.getItemCount()-1){
					tcBCB.setSelectedIndex(tcBCB.getSelectedIndex()+1);
					formularPanelsB.get(jtbB.getSelectedIndex()).grabFocusFirstComponent();
				}
			}
		});
	}
}